﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace Code2html
{
    public class FormatingModule
    {
        string newstr;

        string[] blue;
        string[] green;
        string[] red;
        string[] region;

        public string StyleColor = "";

        void ReadLoad(string path)
        {
            StreamReader sr; // объект потока для чтения
            StringBuilder builder;

            builder = new StringBuilder();
            sr = new StreamReader(path);

            string line;

            string[] linest = new string[] { };
            // читаем строки до конца файла
            line = sr.ReadToEnd();
            if (line != null)
            {
                linest = line.Split(' ', '\r', '\n');
            }
            sr.Close();

            int b = 0, g = 0, r = 0, reg = 0, j = 0;
            for (int ii = 0; ii < linest.Length; ii++)
            {
                if (linest[ii] == "blue:")
                {
                    while (linest[++ii] != "endblue")
                        b++;
                }
                else
                    if (linest[ii] == "green:")
                    {
                        while (linest[++ii] != "endgreen")
                            g++;
                    }
                    else
                        if (linest[ii] == "red:")
                        {
                            while (linest[++ii] != "endred")
                                r++;
                        }
                        else
                            if (linest[ii] == "region:")
                            {
                                while (linest[++ii] != "endregion")
                                    reg++;
                            }
            }

            blue = new string[b];
            green = new string[g];
            red = new string[r];
            region = new string[reg];

            for (int i = 0; i < linest.Length; i++)
            {
                if (linest[i] == "blue:")
                {
                    j = 0;
                    i++;
                    while (linest[i] != "endblue")
                    {
                        if (linest[i] == "")
                            i++;
                        else
                        {
                            blue[j] = linest[i];
                            i++;
                            j++;
                        }
                    }
                }
                else
                    if (linest[i] == "green:")
                    {
                        j = 0;
                        i++;
                        while (linest[i] != "endgreen")
                        {
                            if (linest[i] == "")
                                i++;
                            else
                            {
                                green[j] = linest[i];
                                i++;
                                j++;
                            }

                        }

                    }
                    else
                        if (linest[i] == "red:")
                        {
                            j = 0;
                            i++;
                            while (linest[i] != "endred")
                            {
                                if (linest[i] == "")
                                    i++;
                                else
                                {
                                    red[j] = linest[i];
                                    i++;
                                    j++;
                                }

                            }
                        }
                        else
                            if (linest[i] == "region:")
                            {
                                j = 0;
                                i++;
                                while (linest[i] != "endregion")
                                {
                                    if (linest[i] == "")
                                        i++;
                                    else
                                    {
                                        region[j] = linest[i];
                                        i++;
                                        j++;
                                    }

                                }
                            }
            }


        }

        public string Formating(StreamReader str, string slang, string path, string progpath)
        {
            foreach (string file in Directory.GetFiles(progpath + @"\data\languages\", slang + ".txt"))
            {
                if (file != "")
                    ReadLoad(file);

                else
                {
                    throw new System.IO.FileNotFoundException("Файл с параметрами не существует!");
                }
            }

            string s;
            int word = -1;

            if (StyleColor == "")
                newstr = ("<html>" + '\n' + "<head>" + '\n' + "<title>Code2html</title>" + '\n' +
                    "<link rel=\"stylesheet\" href=\"" + slang + ".css" + "\"" + "type=\"text/css\" />" + '\n' +
                    "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-16\">" + '\n' +
                    "<script type=\"text/javascript\" src=\"region.js\"></script>" + '\n' +
                    "</head>" + '\n' + "<body>" + '\n' + "<pre class=\"all\">" + '\n');
            else
                newstr = ("<html>" + '\n' + "<head>" + '\n' + "<title>Code2html</title>" + '\n' +
                    "<link rel=\"stylesheet\" href=\"" + StyleColor + "\"" + "type=\"text/css\" />" + '\n' +
                    "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-16\">" + '\n' +
                    "<script type=\"text/javascript\" src=\"region.js\"></script>" + '\n' +
                    "</head>" + '\n' + "<body>" + '\n' + "<pre class=\"all\">" + '\n');

            int a = 0, b = 0, c = 0, d = 0;
            string words = "";
            string lin = "";
            string regstr, regcom, regmcom, regmcom1, regmcom2, regionS = "", regkey;
            while (str.EndOfStream == false)
            {
                s = str.ReadLine();
                //string[] line = s.Split(' ', ',', '(', ')', ';', '[', ']', '=');

                #region
                //foreach (char linest in s)
                //{
                //    lin += linest.ToString() + ' ';
                //}
                //string[] linestr = lin.Split(' ');
                //lin = "";
                //for (int st = 0; st < linestr.Length; st++)
                //{
                //    if (linestr[st] == red[0])
                //    {
                //        word = s.IndexOf(red[0]);
                //        if (word != -1)
                //        {
                //            s = s.Insert(s.IndexOf(red[0]) + red[0].Length, "</span><span id=&quot;string&quot;>");
                //            c = st;
                //            if (st+1 != linestr.Length)
                //            {
                //                while (linestr[st + 1] != "\"")
                //                    st++;
                //                c = st - c + 1;
                //                foreach (char linest in s)
                //                {
                //                    lin += linest.ToString();
                //                }
                //                string[] lnst = lin.Split('"');
                //                foreach (char linest in lnst[1])
                //                {
                //                    d++;
                //                }
                //                s = s.Insert(s.IndexOf(red[0]) + (d + c), "</span>");

                //                s = s.Insert(word, "<span id=&quot;string&quot;>");
                //            }
                //        }
                //    }

                //}

                //for (int st = 0; st < linest.Length; st++)
                //{
                //    if (linest[st] == red[0])
                //    {
                //        word = s.IndexOf(red[0]);
                //        if (word != -1)
                //        {
                //            //s = s.Insert(s.IndexOf(red[0]) + red[0].Length, "</span><span id=\"string\">");
                //            //s = s.Insert(s.IndexOf(red[0]) + red[0].Length + red[0].Length, "</span>");
                //            s = s.Insert(word, "<span id=\"string\">");

                //        }
                //    }
                //    else
                //        break;
                //}

                //word = s.IndexOf(red[0]);
                //if (word != -1)
                //{
                //    s = s.Insert(word, "<span id=\"string\">");
                //    s += "</span></span>";
                //}
                #endregion

                //regstr = "(" + "@?" + red[0] + ".*?" + red[0] + ")" + "|" + "(" + red[1] + ".*?" + red[1] + ")";
                regstr = "(\"(\\\"|[^\"])*?\"|@\"(\"\"|[^\"])*\")";
                //regstr = @"(""(?<=(""|'))[^""].*?(?=(""|'))""|"+red[2]+")";
                //regstr = "(\"(\\\"|[^\"])*?\")";
                regcom = "(" + green[0] + ".*$" + ")";
                regmcom = "(" + green[1] + ".*" + green[2] + ")";
                regmcom1 = "(" + green[1] + ".*" + ")";
                regmcom2 = "(" + ".*" + green[2] + ")";
                Match isMatch = Regex.Match(s, regcom);
                Match ismMatch = Regex.Match(s, regmcom);
                Match ismMatch1 = Regex.Match(s, regmcom1);
                Match ismMatch2 = Regex.Match(s, regmcom2);


                if (!isMatch.Success && !ismMatch.Success)
                {
                    if (!ismMatch1.Success && b == 0)
                        s = new Regex(regstr, RegexOptions.Compiled).Replace(s, "<span id=letters>$0</span>");
                    else
                    {
                        if (!ismMatch2.Success)
                            b = 1;
                        else
                            b = 0;
                    }
                }

                if (region.Length != 0)
                {
                    word = s.IndexOf(region[0]);
                    if (word != -1)
                    {
                        s = s.Insert(word, "<div id=\"region\" style=\"margin-top:-20px; display:inline\">" +
                            "<img src=\"minus.png\" onclick=\"hide();\" style=\"margin-right:17px\"><span id=keyword>");
                        File.Copy(progpath + @"\data\region.js", Path.GetDirectoryName(path) + @"\region.js", true);
                        File.Copy(progpath + @"\data\plus.png", Path.GetDirectoryName(path) + @"\plus.png", true);
                        File.Copy(progpath + @"\data\minus.png", Path.GetDirectoryName(path) + @"\minus.png", true);
                        s = s.Insert(s.IndexOf(region[0]) + region[0].Length, "</span>");
                    }
                    word = s.IndexOf(region[1]);
                    if (word != -1)
                    {
                        s = s.Insert(word, "<span id=keyword>");
                        s = s.Insert(s.IndexOf(region[1]) + region[1].Length, "</span>");
                        s += "</div>" + "<div id=\"reg\" style=\"display:none\">" +
                            "<img src=\"plus.png\" onClick=\"show();\">" +
                            "&nbsp;&nbsp;<span style=\"color:#CCC; border: solid 1px\">" + region[0] + "</span></div>";
                    }
                }

                regkey = String.Format(@"(?<!""\s?)((\b{0}\b))(?!\s?"")" + "|" + regcom, String.Join(@"\b)|(\b", blue));
                if (!ismMatch.Success)
                {
                    if (!ismMatch1.Success && a == 0)
                    {
                        s = Regex.Replace(s, regkey, "<span id=keyword>$0</span>", RegexOptions.IgnoreCase);
                    }
                    else
                        if (!ismMatch2.Success)
                            a = 1;
                        else
                            a = 0;
                }

                #region blue
                //for (int i = 0; i < blue.Length; i++)
                //{
                //    regkey = "\\b" + blue[i] + "\\b";
                //    Match key = Regex.Match(s, regkey);
                //    if (!isMatch.Success && !ismMatch.Success)
                //    {
                //        if (!ismMatch1.Success && a == 0)
                //        {
                //            if (key.Success)
                //            {
                //                s = new Regex(regkey, RegexOptions.Compiled).Replace(s, "<span id=keyword>$0</span>");
                //            }

                //        }
                //        else
                //            if (!ismMatch2.Success)
                //                a = 1;
                //            else
                //                a = 0;
                //    }
                //    else
                //        break;

                //}
//---------------------------------------------
                //for (int i = 0; i < blue.Length; i++)
                //{
                //    for (int j = 0; j < line.Length; j++)
                //    {
                //        if (!isMatch.Success && !ismMatch.Success)
                //        {
                //            if (!ismMatch1.Success && a == 0)
                //            {
                //                if (line[j] == blue[i])
                //                {
                //                    word = s.IndexOf(blue[i]);
                //                    s = s.Insert(word, "<span id=keyword>");
                //                    s = s.Insert(s.IndexOf(blue[i]) + blue[i].Length, "</span>");
                //                    #region
                //                    //string[] w = words.Split(' ');
                //                    //for (int k = 0; k < w.Length; k++)
                //                    //{
                //                    //    if (w[k] != blue[i])
                //                    //    {
                //                    //        word = s.IndexOf(blue[i]);
                //                    //        s = s.Insert(word, "<span id=\"keyword\">");
                //                    //        s = s.Insert(s.IndexOf(blue[i]) + blue[i].Length, "</span>");
                //                    //        words += blue[i] + ' ';
                //                    //    }
                //                    //    else
                //                    //    {
                //                    //        string[] new_s = s.Split('(', ')', ' ');
                //                    //        for (int l = 0; l < new_s.Length; l++)
                //                    //        {
                //                    //            if (new_s[l] == blue[i])
                //                    //            {
                //                    //                word = s.IndexOf(blue[i]);
                //                    //                s = s.Insert(word, "<span id=\"keyword\">");
                //                    //                s = s.Insert(s.IndexOf(blue[i]) + blue[i].Length, "</span>");
                //                    //            }
                //                    //        }
                //                    //    }
                //                    // }
                //                    #endregion

                //                }
                //            }
                //            else
                //                if (!ismMatch2.Success)
                //                    a = 1;
                //                else
                //                    a = 0;
                //        }
                //        else
                //            break;
                //    }
                //}
                #endregion

                if (isMatch.Success)
                    s = new Regex(regcom, RegexOptions.Compiled).Replace(s, "<span id=comment>$0</span>");
                if (ismMatch.Success)
                    s = new Regex(regmcom, RegexOptions.Compiled).Replace(s, "<span id=comment>$0</span>");
                if (ismMatch1.Success && c == 0)
                    s = new Regex(regmcom1, RegexOptions.Compiled).Replace(s, "<span id=comment>$0");
                else
                {
                    if (!ismMatch2.Success)
                        c = 1;
                    else
                    {
                        c = 0;
                        s = new Regex(regmcom2, RegexOptions.Compiled).Replace(s, "$0</span>");
                    }
                }

                #region green
                //word = s.IndexOf(green[0]);
                //if (word != -1)
                //{
                //    s = s.Insert(word, "<span id=\"comment\">");
                //    s = s.Insert(s.Length, "</span>");
                //}

                //word = s.IndexOf(green[1]);
                //if (word != -1)
                //{
                //    s = s.Insert(word, "<span id=\"comment\">");
                //}
                //word = s.IndexOf(green[2]);
                //if (word != -1)
                //{
                //    s = s.Insert(word, "");
                //    s += "</span>";
                //}
                #endregion

                
                s += '\n';
                newstr += s;
            }

            newstr += ("</pre>" + '\n' + "</body>" + '\n' + "</html>");

            str.Close();
            
            return newstr;
        }

    }
}
